package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.DeptLog;
import com.itheima.service.DeptLogService;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.itheima.pojo.Dept;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Transactional//spring事务管理,rollbackFor指定异常回滚
    @Override
    public void delete(Integer id) throws Exception {
        try {
            deptMapper.deleteById(id);
            empMapper.deleteByDeptId(id);
        } finally {
            DeptLog deptLog=new DeptLog();
            deptLog.setCreateTime(LocalDateTime.now());
            deptLog.setDescription("执行了解释部门的操作，此解散的是"+id+"部门号");
            deptLogService.insertList(deptLog);
        }
    }

    @Override
    public void add(Dept dept1) {
        dept1.setCreateTime(LocalDateTime.now());
        dept1.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept1);
    }

    @Override
    public List<Dept> select(Integer id) {
        return deptMapper.SelectList(id);
    }

    @Override
    public void update(Dept dept2) {
        dept2.setUpdateTime(LocalDateTime.now());
        deptMapper.updateInfo(dept2);
    }


}
